package com.dq.health.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dto.request.common.BaseIdRequest;
import com.dq.health.dto.request.drug.QueryDrugParam;
import com.dq.health.dto.request.drug.SaveDrugRequest;
import com.dq.health.dto.response.drug.DrugInfo;
import com.dq.health.service.IAPIDrugService;
import com.dq.health.utils.SecurityUtils;
import com.dq.health.utils.data.JsonUtil;
import com.dq.health.utils.view.SimpleView;
import com.dq.health.utils.view.StringView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/12
 */
@RestController
@RequestMapping("/drug")
@Api(tags = "药品管理")
public class DrugController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DrugController.class);

    @Autowired
    private IAPIDrugService drugService;

    @GetMapping("/test")
    public String test() {
        LOGGER.error("##### test start");
        LOGGER.warn("##### test start");
        LOGGER.info("##### test start");
        return "hello";
    }

    @PostMapping("/addDrug")
    @ApiOperation("新增药品")
    public SimpleView<Long> addDrug(@RequestBody SaveDrugRequest request) {
        LOGGER.info("#### 新增药品 request:{}", JsonUtil.toString(request));
        SimpleView<Long> view = new SimpleView<>();

        Long userId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(userId);
        Long drugId = drugService.addDrug(request);

        view.success(drugId);
        return view;
    }

    @PostMapping("/updateDrug")
    @ApiOperation("修改药品")
    public StringView updateDrug(@RequestBody SaveDrugRequest request) {
        LOGGER.info("#### 修改药品 request:{}", JsonUtil.toString(request));
        StringView view = new StringView();

        Long userId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(userId);
        drugService.updateDrug(request);

        view.success("修改成功");
        return view;
    }

    @PostMapping("/queryList")
    @ApiOperation("药品列表")
    public SimpleView<IPage<DrugInfo>> queryList(@RequestBody QueryDrugParam request) {
        LOGGER.info("#### 药品列表 request:{}", JsonUtil.toString(request));
        SimpleView<IPage<DrugInfo>> view = new SimpleView<>();

        IPage<DrugInfo> drugInfoIPage = drugService.queryDrugListByParam(request);
        view.success(drugInfoIPage);

        return view;
    }

    @PostMapping("/drugDetail")
    @ApiOperation("药品列表")
    public SimpleView<DrugInfo> queryList(@RequestBody @Valid BaseIdRequest request) {
        LOGGER.info("#### 药品列表 request:{}", JsonUtil.toString(request));
        SimpleView<DrugInfo> view = new SimpleView<>();

        view.success(drugService.getDrugInfo(request.getId()));

        return view;
    }

}
